<?php

class CategoriesController extends AppController {
    var $name = 'Categories'; // riferimento alla cartella delle view
	
    public function beforeFilter(){
    	
		parent::beforeFilter();
		
    }
	
    public function index() {
    	
    	$this->Category->hasMany['CategoryLang']['conditions'] = array('CategoryLang.locale' => Configure::read("Config.language"));
        // $data = $this->Category->find('threaded');
        $data = $this->Category->generateTreeList(null, null, null, '&nbsp;&nbsp;&nbsp;&nbsp;');
		$this->set("categories",$data);

    }
	
	
	public function add() {
		// ricerco tutte le possibili categorie padre da poter assegnare
        $data = $this->Category->generateTreeList(null, null, null, '&nbsp;&nbsp;&nbsp;');
		// print_r($data);die;
		$this->set("categories",$data);
	}

    public function edit($id) {
    	
		$this->Category->id = $id;
		$this->set('category', $this->Category->read());
		
		// ricerco tutte le possibili categorie padre da poter assegnare
        $data = $this->Category->generateTreeList(array('Category.id !='=>$id), null, null, '&nbsp;&nbsp;&nbsp;');
		// print_r($data);die;
		$this->set("categories",$data);

    }
	
	public function save() {
		    $ds = $this->Category->getDataSource();
			$ds->begin();
		    try {
				$this->Category->setUtente($this->Auth->user());
			    $this->Category->save($this->request->data);
				$ds->commit();
				$this->Session->setFlash(__("Categoria Aggiornata."));
		    } catch (exception $e) {
		    	if (isset($ds)) {
		    		$ds->rollback();
		    	}
		    	CakeLog::error($e->getMessage());
				$this->Session->setFlash(__("Categoria non aggiornata:"). $e->getMessage(),null,null,LOG_ERR);
		    }
		if (isset($this->Category->id)) {
			$this->redirect("/Categories/edit/".$this->Category->id);
		} else {
			$this->redirect($this->referer());
		}
	}
	
    public function delete($id='') {
		if ($id=='')
			$id = $this->request->data['id'];
		$this->Category->delete($id);
		$this->Session->setFlash(__("Categoria eliminata."));
		$this->redirect("/Categories");

    }
	
}

?>